﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.DeepZoomTools;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace DZCollectionCreator
{
    public partial class Form1 : Form
    {
        // NOTE: Change these to the proper folders
        //string source = @"C:\smh_original_pics\prepared_flat";        

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            (sender as Button).Enabled = false;

            string source = txtInput.Text;
            string dest = txtOutput.Text;

            List<string> images = GetImagesInDirectory(source);
            List<string> data = new List<string>();

            int i = 1;


            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode metadataNode = doc.CreateElement("Metadata");
            XmlAttribute versionAttribute = doc.CreateAttribute("version");
            versionAttribute.Value = "1";
            metadataNode.Attributes.Append(versionAttribute);
            doc.AppendChild(metadataNode);



            foreach (var image in images)
            {

                ImageCreator ic = new ImageCreator();

                ic.TileSize = 256;
                ic.TileFormat = ImageFormat.Jpg;
                ic.ImageQuality = 0.95;
                ic.TileOverlap = 0;

                string target = dest + "\\output_images\\" + Path.GetFileNameWithoutExtension(image);

                string sFileName = Path.GetFileNameWithoutExtension(image);
                lblstatus.Text = "Processing: (" + i +"/" + images.Count.ToString() + ")" + sFileName;
                lblstatus.Refresh();

                ic.Create(image, target);
                data.Add(Path.ChangeExtension(target, ".xml"));



                XmlNode imageNode = doc.CreateElement("Image");
                metadataNode.AppendChild(imageNode);

                XmlNode zorderNode = doc.CreateElement("ZOrder");
                zorderNode.AppendChild(doc.CreateTextNode(i.ToString()));
                imageNode.AppendChild(zorderNode);

                XmlNode tagNode = doc.CreateElement("Tag");

                if (sFileName.IndexOf("_") != -1)
                {

                    string sTag = sFileName.Substring(0, sFileName.IndexOf("_"));
                    if (sTag == "READ")
                    {
                        sTag = "Readers";
                    }
                    tagNode.AppendChild(doc.CreateTextNode(sTag));
                    imageNode.AppendChild(tagNode);
                }

                i++;
            }

            CollectionCreator cc = new CollectionCreator();

            cc.TileSize = 256;
            cc.TileFormat = ImageFormat.Jpg;
            cc.MaxLevel = 8;
            cc.ImageQuality = 0.95;
            cc.Create(data, dest + "\\output");



            doc.Save(dest + "\\Metadata.xml");
            lblstatus.Text = "Done!";
            (sender as Button).Enabled = true;
        }

        private static List<string> GetImagesInDirectory(string path)
        {
            return GetImagesInDirectory(new DirectoryInfo(path));
        }

        private static List<string> GetImagesInDirectory(DirectoryInfo di)
        {
            List<string> images = new List<string>();

            // get all the images in this directory first
            foreach (var fi in di.GetFiles("*.jpg"))
            {
                images.Add(fi.FullName);
            }

            // get all the directories with their images
            foreach (var sub in di.GetDirectories())
            {
                images.AddRange(GetImagesInDirectory(sub));
            }

            return images;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {



    
        }

        private void btnSelectInput_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();

            if (f.ShowDialog() == DialogResult.OK)
            {
                txtInput.Text = f.SelectedPath;
            }
        }

        private void btnSelectOutput_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();

            if (f.ShowDialog() == DialogResult.OK)
            {
                txtOutput.Text = f.SelectedPath;
            }
        }
    }
}
